package com.teamwork.demo.controller;

import com.teamwork.demo.entity.User;
import com.teamwork.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
    @Autowired
    public UserService userService;

    @GetMapping("/check")
    public String checkRegistration(@RequestParam("ID") Integer id) {
        boolean isRegistered = userService.isUserRegistered(id);
        if (isRegistered) {
            return "redirect:/login";
        } else {
            return "redirect:/register";
        }
    }

    @GetMapping("/register")
    @ResponseBody
    public User registerUser(User user) {
        return userService.registerNewUser(user);
    }

    @GetMapping ("/login")
    public String login(@RequestParam String uphone, @RequestParam String password) {
        if (userService.islogin(uphone, password)) {
            User user = userService.getUserByPhone(uphone);
            if ("管理员".equals(user.getUtype())) {
                return "redirect:/admin"; // 定向到管理员页面
            } else if ("学生".equals(user.getUtype())) {
                return "redirect:/student"; // 定向到学生页面
            }
        } else {
            return "用户手机号或密码错误";
        }
        return("登录成功");
    }


}

